/**
 * 
 */
package gpl.scotlandyard.android.controllers;

import gpl.scotlandyard.android.R;
import gpl.scotlandyard.android.activities.AbstractActivity;
import gpl.scotlandyard.beans.basics.Board;
import gpl.scotlandyard.services.basics.BoardManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.View;

/** @author Norbert */
public class BasicController implements IController {

  private AbstractActivity activity;

  private BoardManager boardManager;

  @Override
  public AbstractActivity getActivity() {
    return activity;
  }

  @Override
  public void setActivity(AbstractActivity activity) {
    this.activity = activity;
  }

  @Override
  public Board getModel() {
    return boardManager.getBoard();
  }

  /**
   * @param title
   * @param msg
   * @return
   */
  protected void showErrorDialog(int msg) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setCancelable(false);
    builder.setTitle(R.string.app_name);
    builder.setMessage(msg);
    builder.setNeutralButton(android.R.string.ok, new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
      }
    });
    builder.create().show();
  }

  /**
   * @param title
   * @param msg
   * @return
   */
  protected AlertDialog.Builder getYesNoBuilder(int msg) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setCancelable(false);
    builder.setTitle(R.string.app_name);
    builder.setMessage(msg);
    return builder;
  }

  /**
   * @param msg
   * @param choices
   * @param listener
   * @return
   */
  protected AlertDialog.Builder getSelectionDialog(int title, String[] choices, OnClickListener listener) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setItems(choices, listener);
    return builder;
  }

  /**
   * @param msg
   * @param choices
   * @param positiveListener
   * @return
   */
  protected AlertDialog.Builder getDialogWithView(int title, View view, OnClickListener positiveListener) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setView(view);
    builder.setPositiveButton(android.R.string.ok, positiveListener);
    return builder;
  }

  @Override
  public void onViewCreated() {
    // does nothing
  }

  @Override
  public void onBackPressed() {
    getActivity().finish();
  }

  /**
   * @param activity
   *          to set after closing current
   */
  protected void onBackPressed(final Class<? extends Activity> activity) {
    AlertDialog.Builder builder = getYesNoBuilder(R.string.warn_quit_currante_game);
    builder.setPositiveButton(android.R.string.yes, new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        getActivity().startActivity(new Intent(getActivity(), activity));
        getActivity().finish();
      }
    });
    builder.setNegativeButton(android.R.string.no, new OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();
      }
    });
    builder.show();
  }

  @Override
  public BoardManager getBoardManager() {
    return boardManager;
  }

  @Override
  public void setBoardManager(BoardManager boardManager) {
    this.boardManager = boardManager;
  }

}
